package com.qk.management.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.AddCustomerDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.common.exception.CommonException;
import com.qk.common.enums.ParamEnum;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public void updateId(Customer customer) {
        customer.setUpdateTime(java.time.LocalDateTime.now());
        customerMapper.updateById(customer);
    }

    @Override
    public PageResult<Customer> CCtlist(CustomerDTO dto) {
        Page<Customer> page = new Page<>(dto.getPage(), dto.getPageSize());
        baseMapper.CCtlist(page, dto);
        return PageResult.<Customer>builder()
                .total(page.getTotal())
                .rows(page.getRecords())
                .build();
    }
    @Override
    public void add(AddCustomerDTO dto) {
        if (ObjectUtil.isEmpty(dto.getPhone()) || ObjectUtil.isEmpty(dto.getName())) {
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = new Customer();
        customer.setPhone(dto.getPhone());
        customer.setName(dto.getName());
        customer.setChannel(dto.getChannel());
        customer.setSubject(dto.getSubject());
        customer.setGender(dto.getGender());
        customer.setAge(dto.getAge());
        customer.setWechat(dto.getWechat());
        customer.setQq(dto.getQq());
        customer.setDegree(dto.getDegree());
        customer.setJobStatus(dto.getJobStatus());
        customer.setCourseId(dto.getCourseId());
        customer.setCreateTime(java.time.LocalDateTime.now());
        customer.setUpdateTime(java.time.LocalDateTime.now());
        baseMapper.insert(customer);
    }
    
    @Override
    public Customer getById(Integer id) {
        return baseMapper.selectById(id);
    }
}